package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApFollowBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.behavior.dtos.FollowBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApFollowBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @ClassName ApFollowBehaviorServiceImpl
 * @Description TODO
 * @Author AWei
 * @DATE 2021/4/22 21:21
 * @Version 1.0
 */
@Service
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements ApFollowBehaviorService {

    @Resource
    private ApBehaviorEntryService apBehaviorEntryService;

    /**
     * @param dto 关注实体类对象
     * @return 保存结果
     * @Description 保存关注数据
     */
    @Override
    public ResponseResult saveFollowBehavior(FollowBehaviorDto dto) {
        //1. 根据登录用户id查询行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(dto.getUserId(),null);
        if(ObjectUtils.isEmpty(apBehaviorEntry)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2. 同步关注行为数据
        ApFollowBehavior followBehavior = new ApFollowBehavior();
        followBehavior.setEntryId(apBehaviorEntry.getId());
        followBehavior.setCreatedTime(new Date());
        followBehavior.setArticleId(dto.getArticleId());
        followBehavior.setFollowId(dto.getFollowId());
        return ResponseResult.okResult(save(followBehavior));
    }
}
